package javaEE65_day03.StudentTeacherManger.dao;

import javaEE65_day03.StudentTeacherManger.domian.Student;

public class StudentDao {
    //创建保存学生对象的数组
    private static Student[] stus = new Student[5];
    //添加学生信息的方法
    public static boolean addStudent(Student stu) {
        int num = -1;
        for (int i = 0; i < stus.length; i++) {
            Student student = stus[i];
            if (student == null){
                num = i;
                break;
            }
        }
        if (num == -1){
            return false;
        }else {
            stus[num] = stu;
            return true;
        }
    }
    //查看学生信息
    public static Student[] findAllStudent() {
            return stus;
    }
    //删除学生信息
    public static void deleteStudentById(String id) {
        //调取本类中getIndex方法
        int index = getIndex(id);
        //将该索引中的元素用null覆盖
        stus[index] = null;
    }
    //修改学生信息
    public static void updateStudent(String id, Student newstu) {
        //调用本类中getIndex方法获取用户输入的学号的索引位置
       int index = getIndex(id);
       //将该索引位置, 使用newstu对象替换
        stus[index] = newstu;
    }
    //根据学号查找所在的索引位置
    private static int getIndex(String id) {
        int index = -1;
        //遍历数组stus
        for (int i = 0; i < stus.length; i++) {
            Student stu = stus[i];
            if (stu != null && stu.getStudentNumber().equals(id) ){
                return i;
            }
        }
        return index;
    }

}
